
function SetPref( changeWhat ) {
	//print("Mouse is over: " + changeWhat);
	EnterOver = changeWhat;
	return true;
}

function LoadPref() {
	//print("Loading Preferences: " + EnterOver);
	if(EnterOver>=0) {
		preferences.Dev_ID.value = dev[EnterOver].id;
		preferences.Dev_Font.value = dev[EnterOver].text_font;
		preferences.Dev_FGColor.value = dev[EnterOver].text_name.color;
		preferences.Dev_BGColor.value = dev[EnterOver].img_bg3.colorize;
		preferences.Dev_DLabel.value = dev[EnterOver].text_name_option;
		preferences.Dev_DAlign.value = dev[EnterOver].text_name.alignment;
		preferences.Dev_ILabel.value = dev[EnterOver].text_size_option;
		preferences.Dev_IAsign.value = dev[EnterOver].text_size.alignment;
		preferences.Dev_Graph.value = dev[EnterOver].text_graph_option;
	}
}


function ChangeDrive( changeWhat ) {
	var tmp = prompt("Change Drive to:",dev[changeWhat].id);
	if(tmp.length==2) {
		if(filesystem.getDisplayName(tmp)) {
			dev[changeWhat].id = tmp;
			dev[changeWhat].name = "dev_" + tmp[0];
			dev[changeWhat].init();
			//reloadWidget();
		} else alert("No Valid Drive");
	} else alert("Wrong imput! Try C:");
}

function SpawnNew( changeWhat ) {
	var newi = dev.length;
	//while(dev[newi]) newi++;
	
	dev[newi] = new NewDevice(dev[changeWhat].id,newi);
	dev[newi].y = dev[changeWhat].y + 33;
	dev[newi].vOffset = dev[changeWhat].vOffset + 33;
	dev[newi].init();
	dev[newi].window.visible = true;
}



function RemoveW( changeWhat ) {
	delete dev[changeWhat];
}



function ReOrder1( changeWhat ) {
	for (i in dev) {
		dev[i].x = dev[changeWhat].window.hOffset;
		dev[i].y = dev[changeWhat].window.vOffset + (33 * (i - changeWhat));
		dev[i].window.hOffset = dev[changeWhat].window.hOffset;
		dev[i].window.vOffset = dev[changeWhat].window.vOffset + (33 * (i - changeWhat));
		dev[i].init();
	}
}

function ReOrder2( changeWhat ) {
	for (i in dev) {
		dev[i].img_bg3.hOffset = dev[changeWhat].img_bg3.hOffset;
		dev[i].init();
	}
}

function SwitchGraph( changeWhat ) {
	dev[changeWhat].text_graph_option++;
	if(dev[changeWhat].text_graph_option>2) dev[changeWhat].text_graph_option=0;
	dev[changeWhat].init();
}

function SwitchAlignmentName( changeWhat ) {
	if ( dev[changeWhat].text_name.alignment=="left" ) dev[changeWhat].text_name.alignment="center";
	else dev[changeWhat].text_name.alignment="left";
	dev[changeWhat].init();
}

function SwitchAlignmentSize( changeWhat ) {
	if ( dev[changeWhat].text_size.alignment=="left" ) dev[changeWhat].text_size.alignment="center";
	else dev[changeWhat].text_size.alignment="left";
	dev[changeWhat].init();
}

function SwitchTypeName( changeWhat ) {
	dev[changeWhat].text_name_option++;
	if(dev[changeWhat].text_name_option>1) dev[changeWhat].text_name_option=0;
	dev[changeWhat].init();
}

function SwitchTypeSize( changeWhat ) {
	dev[changeWhat].text_size_option++;
	if(dev[changeWhat].text_size_option>5) dev[changeWhat].text_size_option=0;
	dev[changeWhat].init();
}

function AllGraph() {
	for (i in dev)
		dev[i].graph();
}

function AllChanged() {
	for (i in dev)
		dev[i].changed();
}

function ReSize_Move( changeWhat ) {
	change = system.event.hOffset - clickGlobal;
	newpos = oldPosition + change;
	if( (newpos>=size_min)&&(newpos<=size_max)  ) {
		dev[changeWhat].img_bg3.hOffset = oldPosition + change;

		dev[changeWhat].img_bg2.width = dev[changeWhat].img_bg3.hOffset - dev[changeWhat].img_bg2.hOffset +3 ;
		for (var i = graph_width - 1; i >= 0 ; --i) {
			dev[changeWhat].img_graph_read[i].hOffset = dev[changeWhat].img_bg3.hOffset + graph_hOffset + i;
			dev[changeWhat].img_graph_write[i].hOffset = dev[changeWhat].img_bg3.hOffset + graph_hOffset + i;
		}
		dev[changeWhat].text_read.hOffset = dev[changeWhat].img_bg3.hOffset + dev[changeWhat].img_bg3.width - 3;
		dev[changeWhat].text_write.hOffset = dev[changeWhat].img_bg3.hOffset + dev[changeWhat].img_bg3.width - 3;
		
		//dev[changeWhat].init();
	}

}

function ReSize_Down( changeWhat ) {
	oldPosition = dev[changeWhat].img_bg3.hOffset;
	clickGlobal = system.event.hOffset;
}

function ReSize_Up( changeWhat ) {
	dev[changeWhat].init();
	dev[changeWhat].save();
}

function WW_Move( changeWhat ) {
	change_x = eval(system.event.hOffset + "- (" +  new_click_x + ")");
	change_y = eval(system.event.vOffset + "- (" +  new_click_y + ")");
	dev[changeWhat].window.hOffset += change_x;
	dev[changeWhat].window.vOffset += change_y;
	dev[changeWhat].x = dev[changeWhat].window.hOffset;
	dev[changeWhat].y = dev[changeWhat].window.vOffset;
}

function WW_Down( changeWhat ) {
	old_window_x = dev[changeWhat].window.hOffset;
	old_window_y = dev[changeWhat].window.vOffset;
	new_click_x = system.event.hOffset;
	new_click_y = system.event.vOffset;
}

function WW_Up( changeWhat ) {
	dev[changeWhat].init();
	dev[changeWhat].save();
	new_click_x = 0
	new_click_y = 0
}


function DoubleClick( openWhat ) {
	filesystem.open(dev[openWhat].id+"\\");
}

function DeviceInit() {
	MyHDDStatic(this.hdd,this.id);
	this.hdd.VolumeNameD = false;

	if( (this.hdd.VolumeName!=null) && (this.hdd.Size>0)   ) {
		this.img_bg2.width = this.img_bg3.hOffset - this.img_bg2.hOffset +3 ;
		this.img_bg3.width = 31;

		this.text_name.width = this.img_bg3.hOffset - this.img_bg2.hOffset - 4;
		this.text_size.width = this.img_bg3.hOffset - this.img_bg2.hOffset - 4;
		this.text_read.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;
		this.text_write.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;


		if(this.text_name.alignment=="left") this.text_name.hOffset = 32;
		else this.text_name.hOffset = ( 32 + ( this.text_name.width / 2 ) );
		
		if(this.text_size.alignment=="left") this.text_size.hOffset = 32;
		else this.text_size.hOffset = ( 32 + ( this.text_size.width / 2 ) );


		if(this.text_name_option==1)
			this.text_name.data = this.id + "\\" + this.hdd.VolumeName;
		if(this.text_name_option==2)
			this.text_name.data = filesystem.getDisplayName(this.id+"\\");
		else
			this.text_name.data = this.hdd.VolumeName;


		if(this.text_graph_option!=0) {
			this.text_read.data = "";
			this.text_write.data = "";
		}
		if(this.text_graph_option!=1) {
			for (var i = graph_width - 1; i >= 0 ; --i) {
				this.img_graph_read[i].height = 0;
				this.img_graph_write[i].height = 0;
			}
		}
		if(this.text_graph_option==1) {
			for (var i = graph_width - 1; i >= 0 ; --i) {
				this.img_graph_read[i].hOffset = this.img_bg3.hOffset + graph_hOffset + i;
				this.img_graph_write[i].hOffset = this.img_bg3.hOffset + graph_hOffset + i;
			}
		}
		if(this.text_graph_option==2) {
			this.img_bg3.src="Resources/BackGround/end.png";
			this.img_bg3.width=8;
			this.img_bg3.vOffset=1;
		} else {
			this.img_bg3.src="Resources/BackGround/box.png";
			this.img_bg3.width=32;
			this.img_bg3.vOffset=0;
		}

		this.changed();
	} else {
		this.img_bg2.width = 0;
		this.img_bg3.width = 0;
		this.text_name.data = "";
		this.text_size.data = "";
		this.text_read.data = "";
		this.text_write.data = "";

	}

	this.img_icon.src = this.id + "\\";
	this.img_icon.reload();
}

function DeviceChanged() {
	MyHDDStatic(this.hdd,this.id);

	if(this.hdd.VolumeNameD==true) this.init();

	if( (this.hdd.VolumeName!=null) && (this.hdd.Size>0) ) {
		if(this.text_size_option==5)
			this.text_size.data = fsize(this.hdd.Size);
		else if(this.text_size_option==4)
			this.text_size.data = fsize(this.hdd.Size) + " Total";
		else if(this.text_size_option==3)
			this.text_size.data = fsize(this.hdd.FreeSpace);
		else if(this.text_size_option==2)
			this.text_size.data = fsize(this.hdd.FreeSpace) + " Free";
		else if(this.text_size_option==1)
			this.text_size.data = fsize(this.hdd.FreeSpace) + " of " + fsize(this.hdd.Size) + " Free";
		else 
			this.text_size.data = fsize(this.hdd.FreeSpace) + " / " + fsize(this.hdd.Size);
	}
}

function DeviceGraph() {
	MyHDDDynamic(this.hdd,this.id);

	if(new_click_x == 0) {
		this.window.hOffset = this.x;
		this.window.vOffset = this.y;
	}

	if( (this.hdd.VolumeName!=null) && (this.hdd.Size>0)  ) {
		if(this.text_graph_option==0) {
			this.text_read.data = fsizeX(this.hdd.DiskReadBytesPerSecD);
			this.text_write.data = fsizeX(this.hdd.DiskWriteBytesPerSecD);
		}
		if(this.text_graph_option==1) {
			//Read
			var new_height = this.hdd.DiskReadBytesPerSecD * graph_height / this.hist_max_read;
			if(new_height>graph_height) { this.hist_max_read = this.hdd.DiskReadBytesPerSecD; new_height=graph_height; }
			
			var tmp_max = graph_height;
			
			for (var i = graph_width - 1; i > 0 ; --i) {
				this.hist_graph_read[i] = this.hist_graph_read[i-1];
				this.img_graph_read[i].height = this.hist_graph_read[i] * graph_height / this.hist_max_read;
				this.img_graph_read[i].vOffset = graph_vOffset + (graph_height - this.img_graph_read[i].height);
				if(tmp_max<this.hist_graph_read[i]) tmp_max = this.hist_graph_read[i];
			}
			
			this.img_graph_read[0].vOffset = graph_vOffset + (graph_height - new_height);
			this.img_graph_read[0].height  = new_height;
			this.hist_graph_read[0] = this.hdd.DiskReadBytesPerSecD;
			if(tmp_max<this.hist_graph_read[0]) tmp_max = this.hist_graph_read[0];
			if(tmp_max<this.hist_max_read) this.hist_max_read=tmp_max;
	
			//Write 
			new_height = this.hdd.DiskWriteBytesPerSecD * graph_height / this.hist_max_write;
			if(new_height>graph_height) { this.hist_max_write = this.hdd.DiskWriteBytesPerSecD; new_height=graph_height; }
			
			tmp_max = graph_height;
			for (i = graph_width - 1; i > 0 ; --i) {
				this.hist_graph_write[i] = this.hist_graph_write[i-1];
				this.img_graph_write[i].height = this.hist_graph_write[i] * graph_height / this.hist_max_write;
				if(tmp_max<this.hist_graph_write[i]) tmp_max = this.hist_graph_write[i];
			}
			
			this.img_graph_write[0].height  = new_height;
			this.hist_graph_write[0] = this.hdd.DiskWriteBytesPerSecD;
			if(tmp_max<this.hist_graph_write[0]) tmp_max = this.hist_graph_write[0];
			if(tmp_max<this.hist_max_write) this.hist_max_write=tmp_max;
		}
	}
}


function NewDevice(id,num) {
	this.id = id;
	this.name = "dev_" + id[0];
	this.num = num;

	this.hdd = new MyHDD();

	this.window = new Window();
	this.window.name = this.name + "window";
	this.window.title = "HDD Monitor";
	this.window.visible = false;
	
	this.x = this.window.hOffset;
	this.y = this.window.vOffset;
	
	this.window.onMouseEnter = "SetPref("+this.num+");";
	//this.window.onMouseExit = "SetPref(-1);";


	this.img_bg1 = new Image();
	this.img_bg1.name = this.name + "img_bg1";
	this.img_bg1.window = this.window;
	this.img_bg1.src = "Resources/BackGround/box.png";
	this.img_bg1.colorize = "#9F7F7F"
	this.img_bg1.onMultiClick = "DoubleClick("+this.num+");";
	this.img_bg1.zOrder=10;

	this.img_bg1.onMouseDown = "WW_Down("+this.num+");";
	this.img_bg1.onMouseMove = "WW_Move("+this.num+");";
	this.img_bg1.onMouseUp = "WW_Up("+this.num+");";

	this.img_bg2 = new Image();
	this.img_bg2.name = this.name + "img_bg2";
	this.img_bg2.window = this.window;
	this.img_bg2.src = "Resources/BackGround/line.png";
	this.img_bg2.vOffset = 1;
	this.img_bg2.hOffset = 30;
	this.img_bg2.width = 101;
	this.img_bg2.colorize = "#9F7F7F"
	this.img_bg2.zOrder=5;

	this.img_bg2.onMouseDown = "WW_Down("+this.num+");";
	this.img_bg2.onMouseMove = "WW_Move("+this.num+");";
	this.img_bg2.onMouseUp = "WW_Up("+this.num+");";


	this.img_bg3 = new Image();
	this.img_bg3.name = this.name + "img_bg3";
	this.img_bg3.window = this.window;
	this.img_bg3.src = "Resources/BackGround/box.png";
	this.img_bg3.hOffset = 130;
	this.img_bg3.colorize = "#9F7F7F"
	this.img_bg3.zOrder=15;
	
	this.img_bg3.onMouseDown = "ReSize_Down("+this.num+");";
	this.img_bg3.onMouseMove = "ReSize_Move("+this.num+");";
	this.img_bg3.onMouseUp = "ReSize_Up("+this.num+");";

	this.img_icon = new Image();
	this.img_icon.name = this.name + "img_icon";
	this.img_icon.window = this.window;
	//this.img_icon.src = "Resources/DriveIcon/3.png";
	this.img_icon.height = 24;
	this.img_icon.width = 24;
	this.img_icon.useFileIcon=1;
	this.img_icon.src = this.id + "\\";
	this.img_icon.vOffset = 3;
	this.img_icon.hOffset = 4;
	this.img_icon.onMultiClick = "DoubleClick("+this.num+");";
	this.img_icon.zOrder=12;

	this.img_graph_read = new Array(graph_width);
	this.hist_graph_read = new Array(graph_width);
	this.hist_max_read = graph_height;
	
	this.img_graph_write = new Array(graph_width);
	this.hist_graph_write = new Array(graph_width);
	this.hist_max_write = graph_height;


	for (var i = graph_width - 1; i >= 0 ; --i) {
		this.hist_graph_read[i] = 0;
		this.hist_graph_write[i] = 0;
	
		this.img_graph_read[i] = new Image();
		this.img_graph_read[i].window = this.window;
		this.img_graph_read[i].src = "Resources/Graph/read.gif";
		this.img_graph_read[i].hOffset = this.img_bg3.hOffset + graph_hOffset + i;
		this.img_graph_read[i].vOffset = this.img_bg3.vOffset + graph_vOffset + graph_height;
		this.img_graph_read[i].width = 1;
		this.img_graph_read[i].height = 0;
		
		this.img_graph_write[i] = new Image();
		this.img_graph_write[i].window = this.window;
		this.img_graph_write[i].src = "Resources/Graph/write.gif";
		this.img_graph_write[i].hOffset = this.img_bg3.hOffset + graph_hOffset + i;
		this.img_graph_write[i].vOffset = this.img_bg3.vOffset + graph_vOffset + graph_height;
		this.img_graph_write[i].width = 1;
		this.img_graph_write[i].height = 0;
	}


	this.text_name = new Text();
	this.text_name.name = this.name + "text_name";
	this.text_name.window = this.window;
	this.text_name.vOffset = 13;
	this.text_name.hOffset = 32;
	this.text_name.width = this.img_bg3.hOffset - this.text_name.hOffset - 4;
	this.text_name.data = "VolumeName";
	this.text_name.alignment="left";
	this.text_name.font="Tahoma";
	this.text_name.size=12;
	this.text_name.style="bold";
	this.text_name.color="#ffffff";
	//this.text_name.truncation="end";
	//this.text_name.scrolling="autoLeft";
	this.text_name_option = 0;

	this.text_size = new Text();
	this.text_size.name = this.name + "text_size";
	this.text_size.window = this.window;
	this.text_size.vOffset = 25;
	this.text_size.hOffset = 33;
	this.text_size.width = this.img_bg3.hOffset - this.text_name.hOffset - 4;
	this.text_size.data = "Free Space";
	this.text_size.alignment="left";
	this.text_size.font="Tahoma";
	this.text_size.size=10;
	this.text_size.style="bold";
	this.text_size.color="#ffffff";
	//this.text_size.truncation="end";
	//this.text_size.scrolling="autoLeft";
	this.text_size_option = 0;

	this.text_read = new Text();
	this.text_read.name = this.name + "text_read";
	this.text_read.window = this.window;
	this.text_read.vOffset = 13;
	this.text_read.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;
	this.text_read.data = "0B";
	this.text_read.alignment="right";
	this.text_read.font="Tahoma";
	this.text_read.size=10;
	this.text_read.style="bold";
	this.text_read.color="#ffffff";

	this.text_write = new Text();
	this.text_write.name = this.name + "text_write";
	this.text_write.window = this.window;
	this.text_write.vOffset = 25;
	this.text_write.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;
	this.text_write.data = "0B";
	this.text_write.alignment="right";
	this.text_write.font="Tahoma";
	this.text_write.size=10;
	this.text_write.style="bold";
	this.text_write.color="#ffffff";

	this.text_font = this.text_name.font + " " + this.text_name.style;

	this.text_graph_option = 0;

	this.save = DeviceSavePref;
	this.init = DeviceInit;
	this.changed = DeviceChanged;
	this.graph = DeviceGraph;

	var LoadPref = DeviceLoadPref(this.name);
	if(LoadPref) {
		this.window.hOffset = LoadPref[0];
		this.window.vOffset = LoadPref[1];
		this.x = LoadPref[0];
		this.y = LoadPref[1];
		this.img_bg3.hOffset = LoadPref[2];
		this.text_name_option = LoadPref[3];
		this.text_size_option = LoadPref[4];
		this.text_graph_option = LoadPref[5];
		this.text_name.font = LoadPref[6];
		this.text_name.style = LoadPref[7];
		this.text_name.color = LoadPref[8];
		this.text_size.font = LoadPref[6];
		this.text_size.style = LoadPref[7];
		this.text_size.color = LoadPref[8];
		this.text_read.font = LoadPref[6];
		this.text_read.style = LoadPref[7];
		this.text_read.color = LoadPref[8];
		this.text_write.font = LoadPref[6];
		this.text_write.style = LoadPref[7];
		this.text_write.color = LoadPref[8];
		this.img_bg1.colorize = LoadPref[9];
		this.img_bg2.colorize = LoadPref[9];
		this.img_bg3.colorize = LoadPref[9];
		this.text_name.alignment = LoadPref[10];
		this.text_size.alignment = LoadPref[11];
		this.text_font = LoadPref[12];
	}

var TmpWidth = this.img_bg3.hOffset + this.img_bg3.width + 2;
var TmpHeight = this.img_bg3.height + 2;

if ((this.window.hOffset + TmpWidth ) > screen.availWidth)
	this.window.hOffset = screen.availWidth - TmpWidth;

if ((this.window.vOffset + TmpHeight) > screen.availHeight)
	this.window.vOffset = screen.availHeight - TmpHeight;


if (this.window.hOffset < screen.availLeft)
	this.window.hOffset = screen.availLeft;

if (this.window.vOffset < screen.availTop)
	this.window.vOffset = screen.availTop;




	this.img_bg2.width = this.img_bg3.hOffset - this.img_bg1.hOffset + 3;
	this.text_name.width = this.img_bg3.hOffset - this.img_bg2.hOffset - 4;
	this.text_size.width = this.img_bg3.hOffset - this.img_bg2.hOffset - 4;
	this.text_read.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;
	this.text_write.hOffset = this.img_bg3.hOffset + this.img_bg3.width - 3;


	if(this.text_name.alignment=="left") this.text_name.hOffset = 32;
	else this.text_name.hOffset = ( 32 + ( this.text_name.width / 2 ) );
	
	if(this.text_size.alignment=="left") this.text_size.hOffset = 32;
	else this.text_size.hOffset = ( 32 + ( this.text_size.width / 2 ) );


	var itemsA = new Array();
	itemsA[0] = new MenuItem();
	itemsA[0].title = "Monitor: Re-Order all";
	itemsA[0].onSelect = "ReOrder1("+this.num+");";
	itemsA[1] = new MenuItem();
	itemsA[1].title = "Monitor: Re-Size all";
	itemsA[1].onSelect = "ReOrder2("+this.num+");";


	itemsA[2] = new MenuItem();
	itemsA[2].title = "-";
	itemsA[2].onSelect = "alert('Error');";

	itemsA[3] = new MenuItem();
	itemsA[3].title = "Monitor: Spawn new One";
	itemsA[3].onSelect = "SpawnNew("+this.num+");";

	itemsA[4] = new MenuItem();
	itemsA[4].title = "Monitor: Remove this One";
	itemsA[4].onSelect = "RemoveW("+this.num+");";
	this.img_bg1.contextMenuItems = itemsA;


	var itemsB = new Array();
	itemsB[0] = new MenuItem();
	itemsB[0].title = "Performance: Change Content (Text/Graph/None)";
	itemsB[0].onSelect = "SwitchGraph("+this.num+");";
	this.img_bg3.contextMenuItems = itemsB;


	var itemsC = new Array();
	itemsC[0] = new MenuItem();
	itemsC[0].title = "Name: Change Alignment (Left/Center)";
	itemsC[0].onSelect = "SwitchAlignmentName("+this.num+");";

	itemsC[1] = new MenuItem();
	itemsC[1].title = "Name: Change Content";
	itemsC[1].onSelect = "SwitchTypeName("+this.num+");";

	itemsC[2] = new MenuItem();
	itemsC[2].title = "-";
	itemsC[2].onSelect = "alert('Error');";

	itemsC[3] = new MenuItem();
	itemsC[3].title = "Monitor: Change this Device";
	itemsC[3].onSelect = "ChangeDrive("+this.num+");";

	itemsC[4] = new MenuItem();
	itemsC[4].title = "Monitor: Spawn new One";
	itemsC[4].onSelect = "SpawnNew("+this.num+");";

	itemsC[5] = new MenuItem();
	itemsC[5].title = "Monitor: Remove this One";
	itemsC[5].onSelect = "RemoveW("+this.num+");";


	this.text_name.contextMenuItems = itemsC;

	var itemsD = new Array();
	itemsD[0] = new MenuItem();
	itemsD[0].title = "Space: Change Alignment (Left/Center)";
	itemsD[0].onSelect = "SwitchAlignmentSize("+this.num+");";
	itemsD[1] = new MenuItem();
	itemsD[1].title = "Space: Change Content";
	itemsD[1].onSelect = "SwitchTypeSize("+this.num+");";
	this.text_size.contextMenuItems = itemsD;


}

function DeviceSavePref() {
	var saveFileName = system.widgetDataFolder + "/" + this.name + ".ini";
	var myData = new Array();
	myData[0] = this.x;
	myData[1] = this.y;
	myData[2] = this.img_bg3.hOffset;
	myData[3] = this.text_name_option;
	myData[4] = this.text_size_option;
	myData[5] = this.text_graph_option;
	myData[6] = this.text_name.font;
	myData[7] = this.text_name.style;
	myData[8] = this.text_name.color;
	myData[9] = this.img_bg3.colorize;
	myData[10] = this.text_name.alignment;
	myData[11] = this.text_size.alignment;
	myData[12] = this.text_font;
	filesystem.writeFile( saveFileName, myData );
}

function DeviceLoadPref(name) {
	var saveFileName = system.widgetDataFolder + "/" + name + ".ini";
	if (filesystem.itemExists(saveFileName)) {
		var lines = filesystem.readFile( saveFileName, true );
		return lines;
	} else {
		return false;
	}
}

function MyHDD() {
	this.VolumeName = "";
	this.VolumeNameL = "";
	this.VolumeNameD = false;
	this.VolumeSerialNumber = "";
	this.ProviderName = "";
	this.DriveType = 0;
	this.MediaType = 0;
	this.FileSystem = "";
	this.Size = 0;
	this.FreeSpace = 0;
	this.Compressed = 0;
	
	this.DriveTypeS = "";
	this.MediaTypeS = "";
	
	this.PercentIdleTime = 0;
	this.PercentIdleTime_Base = 0;
	
	this.DiskReadBytesPerSec = 0;
	this.DiskWriteBytesPerSec = 0;
	this.DiskReadBytesPerSecL = 0;
	this.DiskWriteBytesPerSecL = 0;
	this.DiskReadBytesPerSecD = 0;
	this.DiskWriteBytesPerSecD = 0;
}

function MyHDDStatic(target,source_id) {
	var source = wmi.Get( 'Win32_LogicalDisk="'+source_id+'"' );
	target.VolumeName = source.Properties_.Item( "VolumeName" ).Value;
	target.VolumeSerialNumber = source.Properties_.Item( "VolumeSerialNumber" ).Value;
	target.ProviderName = source.Properties_.Item( "ProviderName" ).Value;
	
	target.DriveType = source.Properties_.Item( "DriveType" ).Value;
	target.MediaType = source.Properties_.Item( "MediaType" ).Value;
	
	target.FileSystem = source.Properties_.Item( "FileSystem" ).Value;
	target.Size = source.Properties_.Item( "Size" ).Value;
	target.FreeSpace = source.Properties_.Item( "FreeSpace" ).Value;
	target.Compressed = source.Properties_.Item( "Compressed" ).Value;

	target.DriveTypeS = GetDriveType(target.DriveType);
	target.MediaTypeS = GetMediaType(target.MediaType);

	if(target.VolumeName!=target.VolumeNameL) {
		target.VolumeNameL=target.VolumeName;
		target.VolumeNameD=true;
	}

	if(target.VolumeName == null) target.VolumeName = filesystem.getDisplayName(source_id+"\\");

	return true;
}

function MyHDDDynamic(target,source_id) {
	var source = wmi.Get( 'Win32_PerfRawData_PerfDisk_LogicalDisk="'+source_id+'"' );
	target.DiskReadBytesPerSec = source.Properties_.Item( "DiskReadBytesPerSec" ).Value
	target.DiskWriteBytesPerSec = source.Properties_.Item( "DiskWriteBytesPerSec" ).Value
	target.PercentIdleTime = source.Properties_.Item( "PercentIdleTime" ).Value
	target.PercentIdleTime_Base = source.Properties_.Item( "PercentIdleTime_Base" ).Value

	if(target.DiskReadBytesPerSecL>target.DiskReadBytesPerSec) target.DiskReadBytesPerSecL = target.DiskReadBytesPerSec;

	if(target.DiskReadBytesPerSecL>0) target.DiskReadBytesPerSecD = target.DiskReadBytesPerSec - target.DiskReadBytesPerSecL;
	target.DiskReadBytesPerSecL = target.DiskReadBytesPerSec;

	if(target.DiskWriteBytesPerSecL>target.DiskWriteBytesPerSec) target.DiskWriteBytesPerSecL = target.DiskWriteBytesPerSec;
	
	if(target.DiskWriteBytesPerSecL>0) target.DiskWriteBytesPerSecD = target.DiskWriteBytesPerSec - target.DiskWriteBytesPerSecL;
	target.DiskWriteBytesPerSecL = target.DiskWriteBytesPerSec;

	return true;
}

function GetDriveType(source_id) {
	//print("DT: " + source_id);
	tmp = -1;
	if(source_id[1]==":") {
		var source = wmi.Get( 'Win32_LogicalDisk="'+source_id+'"' );
		var tmp = source.Properties_.Item( "DriveType" ).Value;
	}
	return tmp;
}

